<%-- 
    Document   : login
    Created on : Feb 3, 2012, 10:33:25 PM
    Author     : PhucAn
--%>

<%@page import="util.SharingUtil"%>
<%@page import="dto.User"%>
<%@page import="util.MD5Encryption"%>
<%@page import="dao.UserDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    if (request.getParameter("submit") != null) {
        UserDAO daoUser = new UserDAO();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean remember = request.getParameter("remember-me") != null;
        if (username.equals("") || password.equals("")) {
            //out.println("Invalid username or password");            
            SharingUtil.showMessage(session, "Invalid username or password");
            return;
        }
        //neu nguoi dung da nhap day du username va password

        MD5Encryption md5 = new MD5Encryption();
        User us = daoUser.getUserByIDAndPassword(username, md5.encrypt(password));
        if (!us.getUsername().equals("")) {
            session.setAttribute(SharingUtil.VAR_S_USER, us);
            if (remember) {
                response.addCookie(new Cookie("username", username));
                response.addCookie(new Cookie("password", password));
                response.addCookie(new Cookie("remember", "true"));
            } else {
                response.addCookie(new Cookie("username", ""));
                response.addCookie(new Cookie("password", ""));
                response.addCookie(new Cookie("remember", "false"));
            }
            //chuyen den trang chu
            response.sendRedirect("index.jsp");
        } else {
            //out.println("Invalid username or password");
            SharingUtil.showMessage(session, "Invalid user or password");

        }
    }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
        <style>
            .login-form{
                margin:auto;
            }
        </style>        
    </head>
    <body>        
        <form method="post" action="#">
            <table class="login-form">
                <tr>
                    <td></td>
                    <td><h1>Login</h1></td>
                </tr>
                <tr>
                    <td>Username: </td>
                    <td><input type="text" name="username" value="${cookie.remember.value=='true'?cookie.username.value:''}"/></td>
                </tr>
                <tr>
                    <td>Password: </td>
                    <td><input type="password" name="password" value="${cookie.remember.value=='true'?cookie.password.value:''}"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="Login"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                      
                    </td>
                </tr>
                <tr>
                    <td></td>                    
                    <td><input type="checkbox" name="remember-me" id="remember-me" ${cookie.remember.value=='true'?" checked ":""}/> Remember me.</td>
                </tr>
                <tr>
                    <td></td>
                    <td><a href="?pcontent=reset-password">Forget your password?</a></td>
                </tr>
            </table>
        </form>

    </body>
</html>

